<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog v-model="roomStore.room.dialogVisible" title="Tips" width="350" @close="CloseFrom">
        <template #header>
            <div class="title">是否结束计费？</div>
        </template>
        <div>
            <div class="from_box">
                <div>开始时间：{{ GetTimeStr2(roomStore.room.StartTime) }}</div>
                <div>结束时间：{{ GetTimeStr2(roomStore.room.EndTime) }}</div>
                <div>本次计费长：{{ GetTimeStr(roomStore.room.EndTime - roomStore.room.StartTime) }}</div>
                <div>本房间单价：{{ roomStore.room.RoomPrice }}元</div>
                <div>本次消费金额：{{ EstimatedCost }}元</div>
                <div style="display: flex; align-items: center">
                    <div>实际收款：</div>
                    <el-input id="123" type="number" style="width: 100px; text-align: center" v-model="PaymentAmount" placeholder="请输入金额" />
                    <div style="margin-left: 10px">元</div>
                </div>
                <div>
                    <div>备注：</div>
                    <el-input type="textarea" v-model="Remarks" placeholder="请输入备注" />
                </div>
            </div>
        </div>
        <template #footer>
            <div>
                <el-button type="success" @click="Confirm()">是</el-button>
                <el-button type="danger" @click="CloseFrom()">否</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { round } from 'lodash-es'
import { ref, watch } from 'vue'
import { baTableApi } from '/@/api/common'
import { useRoomStore } from './workbench'
const roomStore = useRoomStore()

const PaymentAmount = ref<number>(0)
const EstimatedCost = ref<number>(0)
const Remarks = ref<string>('')
// 耗时转文本
const GetTimeStr = (TimeNur: number) => {
    const diff = TimeNur / 1000
    const minutes = Math.floor(diff / 60)
    const seconds = round(diff % 60, 0)
    return `${minutes}分钟${seconds}秒`
}
// 时间转文本
const GetTimeStr2 = (TimeNur: number) => {
    const date = new Date(TimeNur)
    return date.toLocaleString()
}
//关闭窗口
const CloseFrom = () => {
    PaymentAmount.value = 0
    EstimatedCost.value = 0
    Remarks.value = ''
    roomStore.room.dialogVisible = false
}
//确认消费
const Confirm = () => {
    //修改状态为闲置中
    const api = new baTableApi('/admin/room.Info/')
    const params = {
        id: roomStore.room.CurrentRoomId,
        status: '1',
    }
    api.postData('edit', params).then((res) => {
        if (res.code == 1) {
            roomStore.room.list.forEach((item) => {
                if (item.id === roomStore.room.CurrentRoomId) {
                    item.status = 1
                }
            })
        }
    })
    //添加消费记录
    const order = new baTableApi('/admin/order.Info/')
    const pjson = {
        order_no: new Date().getTime().toString(),
        user_id: '1',
        start_time: roomStore.room.StartTime / 1000,
        end_time: roomStore.room.EndTime / 1000,
        time_nurs: GetTimeStr(roomStore.room.EndTime - roomStore.room.StartTime),
        room_info_id: roomStore.room.CurrentRoomId,
        estimated_cost: EstimatedCost.value,
        actual_receipts: PaymentAmount.value,
        order_time: GetTimeStr2(new Date().getTime()),
    }
    order.postData('add', pjson).then((res) => {
        if (res.code == 1) {
            console.log(res)
        }
    })
    //更新数据关闭窗口
    roomStore.updateRoom(roomStore.room)
    CloseFrom()
}
//计算单价

const CalculateUnitPrice = () => {
    const TimeNur = roomStore.room.EndTime - roomStore.room.StartTime
    let price = roomStore.room.RoomPrice
    //30分钟为一个阶段
    const diff = TimeNur / 1000
    const minutes = Math.floor(diff / 60)
    const Nur = Math.floor(minutes / 30)
    console.log(diff, minutes, Nur)
    if (minutes - Nur * 30 >= 5) {
        return round(((Nur + 1) * price) / 2, 2)
    } else {
        return round((Nur * price) / 2, 2)
    }
}

// 初始化消费金额
watch(
    () => [roomStore.room.EndTime, roomStore.room.StartTime],
    ([endTime, startTime]) => {
        if (endTime && startTime) {
            PaymentAmount.value = CalculateUnitPrice()
            EstimatedCost.value = PaymentAmount.value
        }
    },
    { immediate: true }
)
// 价格赋值
</script>

<style scoped lang="scss">
.from_box {
    line-height: 30px;
    padding: 10px;
    margin: 0 20px;
}
</style>
